﻿using GameProject.Filters;
using GameProject.Models;
using GameProject.Service;
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebMatrix.WebData;
namespace GameProject.Controllers
{
	public class SnakesGameHub : Hub
	{
		private GameInstanceService gameService = new GameInstanceService();

		/// <summary>
		/// Function that adds a player to a group and then the game with the help of SignalR
		/// </summary>
		/// <param name="groupId">The ID of the game to be played</param>
		public void Join(string groupId)
		{
			//If the URL has an ID in it...
			if (!string.IsNullOrEmpty(groupId))
			{
				SnakesAndLadders snakesAndLadders = new SnakesAndLadders(int.Parse(groupId));

				//As long as the game hasn't got enough players, a player is added to it
				if (snakesAndLadders.NotEnoughPlayers())
				{
					//Add player to group
					Groups.Add(Context.ConnectionId, groupId);
					//Add player to the game
					snakesAndLadders.AddUser();

					//Let player know what his player id is
					Clients.Client(Context.ConnectionId).sendUserId(snakesAndLadders.CurrentNumberOfPlayers);

					//If enough players, start the game
					if (snakesAndLadders.EnoughPlayers())
					{
						snakesAndLadders.StartGame();

						Clients.Group(groupId).startGame(snakesAndLadders.ExpectedNumberOfPlayers, snakesAndLadders.PlayAgainstComputer());

						//If playing against the computer, roll the dice for the computer
						if (snakesAndLadders.PlayAgainstComputer())
						{
							RollDice(int.Parse(groupId), 1);
						}
					}
				}
			}
		}

		/// <summary>
		/// Function that rolls the dice and makes the players move with the help of SignalR.
		/// </summary>
		/// <param name="gameId">The ID of the game to be played</param>
		/// <param name="playerId">The ID of the player</param>
		public void RollDice(int gameId, int playerId)
		{
			SnakesAndLadders snakesAndLadders = new SnakesAndLadders(gameId);

			//The locations of the squares the piece walks on
			List<int> locations = snakesAndLadders.MovePlayer(playerId);

			//Winner!
			if (snakesAndLadders.IsGameOver())
			{
				Clients.Group(gameId.ToString()).sendWinner(playerId, snakesAndLadders.LastDiceResult);
			}
			//If not win, the player moves again.	
			else
			{
				int nextPlayerId = snakesAndLadders.GetNextPlayerId(playerId);

				Clients.Group(gameId.ToString()).makeMove(playerId, locations, nextPlayerId, snakesAndLadders.LastDiceResult);

				//If it's the computer's turn, roll the dice for it
				if (snakesAndLadders.PlayAgainstComputer() && nextPlayerId == 1)
				{
					//Delay for the animation to work
					System.Threading.Thread.Sleep(600 * snakesAndLadders.LastDiceResult);
					RollDice(gameId, 1);
				}
			}
		}

		/// <summary>
		/// Function that makes the chat possible with the help of SignalR.
		/// </summary>
		/// <param name="message">The message being sent</param>
		/// <param name="groupId">The ID of the group</param>
		/// <param name="playerId">The ID of the player</param>
		public void Send(string message, string groupId, int playerId)
		{
			GameInstance game = gameService.GetGameById(int.Parse(groupId));

			//Gets the username
			string userName = game.PieceSnakes[playerId - 1].User.UserName;

			// Call the addMessage method on all clients to send the message
			Clients.Group(groupId).addMessage(userName, message);
		}

		public void Method()
		{
			throw new System.NotImplementedException();
		}
	}
}